# Name: PackwithPyInstaller.py #
# 
# Author: Zhang Huiyuan #
# Creation Date: 2021.11.17 #
# Version: v0.0.2 #
# 

import os

import Main


# CMD: pyinstaller.exe -F -w -i '.\Icon.ico' -n 'SoftwareName_v0.0.1' '.\Main.py'
def CreatePyInstallerPackingCMD():
	# Get global variables from 'Main.py'.
	Main.CreateGlobalVarialbe()

	# Icon path
	szIconPath = GetSoftwareIcon()
	if(szIconPath):
		pass
	else:
		return 0
	# Target '.spec' name
	if(Main.g_bBackend):
		szTargetSPECName = '%s_%s_Backend' %(Main.g_szSoftwareName, Main.g_szVersionNum)
	else:
		szTargetSPECName = '%s_%s' %(Main.g_szSoftwareName, Main.g_szVersionNum)
	# Source '.py' name
	szSourcePYName = '%s/Main.py' %(os.getcwd())

	if(Main.g_bBackend):
		szPyInstallerPackCMD = 'pyinstaller -F -w -i %s -n %s %s' %(szIconPath, szTargetSPECName, szSourcePYName)
	else:
		szPyInstallerPackCMD = 'pyinstaller -F -i %s -n %s %s' %(szIconPath, szTargetSPECName, szSourcePYName)
	return szPyInstallerPackCMD

def GetSoftwareIcon():
	try:
		temp_lsIconsPath = os.listdir('%s/Icons' %(os.getcwd()))
	except Exception as e:
		print('%s\n' %e)
		return 0
	if(temp_lsIconsPath):
		szTargetIconPath = '%s/Icons/%s' %(os.getcwd(), temp_lsIconsPath[0])
		# print('Use icon [%s]\n' %szTargetIconPath)
		return szTargetIconPath
	else:
		# print("Cannot find icon.\nPlease check './Icons/'.\n")
		return 0
	return 0


if __name__ == '__main__':
	szPyInstallerPackCMD = CreatePyInstallerPackingCMD()
	print('Current Version: %s_%s\n' %(Main.g_szSoftwareName, Main.g_szVersionNum))
	szCMDLine = CreatePyInstallerPackingCMD()
	print(szCMDLine if szCMDLine else '')
	print(os.system(szCMDLine) if szCMDLine else "Cannot find icon.\nPlease check './Icons/'.\n")